package com.common.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.common.bean.Personnel;
import com.common.service.CommonService;
import com.core.ssh2.BaseHibernateDao;

public class CommonServiceImpl extends BaseHibernateDao implements
		CommonService {

	@SuppressWarnings("unchecked")
	public List<Map> findMenu(String staffcode) {
		List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){

			@SuppressWarnings("deprecation")
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				
				StringBuffer sql = new StringBuffer();
				sql.append("select * from Menuinfo order by menuid");
				Connection conn = session.connection();
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(sql.toString());
				List<Map> coll = new ArrayList<Map>();
				Map<String,Object> info ;
				while(rs.next()){
					info = new HashMap<String,Object>();
					info.put("id",rs.getInt("menuid"));
					info.put("pid",rs.getInt("menupid"));
					info.put("name",rs.getString("menuname"));
					info.put("url",rs.getString("url"));
					info.put("target",rs.getString("target"));
					coll.add(info);
				}
				return coll;
			}
			
		});
		return list;
	}

	public Object findUser(String uname, String pwd) {
		String hql = "from Personnel u where u.loginname = ? and u.password = ? ";
		List list = this.getHibernateTemplate().find(hql, 
				new Object[]{uname,pwd});
		return (Personnel)(list == null || list.size() == 0 ? null : (Personnel)list.get(0));
	}
}
